import json

from django.utils.deprecation import MiddlewareMixin

from utils.request_util import get_request_ip, get_request_path, get_request_data


class ApiLoggingMiddleware(MiddlewareMixin):
    """
    用于中间件
    """

    def __init__(self, get_response=None):
        super().__init__(get_response)


    @classmethod
    def __handle_request(cls, request):
        request.request_ip = get_request_ip(request)
        request.request_data = get_request_data(request)
        request.request_path = get_request_path(request)

    def __handle_response(self, request, response):
        pass

    def process_view(self, request, view_func, view_args, view_kwargs):
        return

    def process_request(self, request):
        self.__handle_request(request)

    def process_response(self, request, response):
        """
        主要请求处理完之后
        :param request:
        :param response:
        :return:
        """

        self.__handle_response(request, response)
        return response
